package org.gdstash.ui;

import org.gdstash.db.DBStashItem;
import org.gdstash.ui.table.GDItemSimilarTableHeader;
import org.gdstash.ui.table.GDItemSimilarTableModel;
import org.gdstash.ui.util.AdjustablePanel;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import java.awt.*;

public class GDItemInfoSimilarTablePane extends AdjustablePanel {
    private GDItemSimilarTableModel model;
    private GDItemSimilarTableHeader header;
    private JTable table;
    private JScrollPane scrollPane;
    private DBStashItem.DuplicateInfo info;

    public GDItemInfoSimilarTablePane() {
        this.info = null;
        this.init();
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntTable = UIManager.getDefaults().getFont("TableHeader.font");
        if (fntTable == null) {
            fntTable = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntTable = fntTable.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        GDItemSimilarTableHeader.updateToolTips();
        if (this.table == null) {
            this.table = new JTable();
            this.setInfo(this.info);
            if (this.header == null) {
                this.header = new GDItemSimilarTableHeader(this.table.getColumnModel());
                this.table.setTableHeader(this.header);
            }
        }
        this.table.getTableHeader().setFont(fntTable);
        this.table.setFont(fntLabel);
        int size = 12;
        if (GDStashFrame.iniConfig != null) {
            size = GDStashFrame.iniConfig.sectUI.fontSize;
        }
        this.table.setRowHeight((int) (1.5 * size));
        final GDItemSimilarTableModel model = (GDItemSimilarTableModel) this.table.getModel();
        model.adjustUI();
        if (this.scrollPane == null) {
            this.scrollPane = new JScrollPane(this.table);
            this.scrollPane.getVerticalScrollBar().setUnitIncrement(2 * GDStashFrame.iniConfig.sectUI.fontSize);
        }
        this.updateTableColumns();
        final JTableHeader jth = this.table.getTableHeader();
        final int rh = this.table.getRowHeight();
        final int hh = jth.getPreferredSize().height;
        final Dimension dimMax = new Dimension(this.table.getPreferredSize().width, this.table.getRowHeight() + jth.getPreferredSize().height + 6);
        this.setMaximumSize(dimMax);
    }

    private void init() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.scrollPane));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.scrollPane));
        layout.setVerticalGroup(vGroup);
    }

    private void updateTableColumns() {
        this.table.getColumnModel().getColumn(0).setCellRenderer(new GDItemSimilarTableCellRenderer());
        this.table.getColumnModel().getColumn(1).setCellRenderer(new GDItemSimilarTableCellRenderer());
        this.table.getColumnModel().getColumn(2).setCellRenderer(new GDItemSimilarTableCellRenderer());
        this.table.getColumnModel().getColumn(3).setCellRenderer(new GDItemSimilarTableCellRenderer());
        this.table.getColumnModel().getColumn(4).setCellRenderer(new GDItemSimilarTableCellRenderer());
        this.table.getColumnModel().getColumn(5).setCellRenderer(new GDItemSimilarTableCellRenderer());
    }

    public void setInfo(final DBStashItem.DuplicateInfo info) {
        this.info = info;
        this.model = new GDItemSimilarTableModel(info);
        this.table.setModel(this.model);
        this.updateTableColumns();
    }

    private class GDItemSimilarTableCellRenderer extends DefaultTableCellRenderer {
        public GDItemSimilarTableCellRenderer() {
            this.setHorizontalAlignment(0);
        }

        @Override
        public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int col) {
            final JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
            final GDItemSimilarTableModel model = (GDItemSimilarTableModel) table.getModel();
            label.setForeground(model.getCellTextColor(row, col));
            label.setBackground(model.getCellBackgroundColor(row, col));
            return label;
        }
    }
}
